import User.User;

import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class Treatment {
    //判断注册数据是否为空
    public static boolean islegitimate(String name, String email, String passwd, String passwd2)
    {
        if(name == null || email == null || passwd == null || passwd2 == null)
            return false;
        if(!passwd.equals(passwd2))
            return false;
        return true;
    }
    //判断邮箱是否已经被注册过
    public static boolean isRepeat(String email)
    {
        try {
            File file = new File("user.txt");
            BufferedReader buff = new BufferedReader(new FileReader(file));
            String s = "";
            while ((s = buff.readLine()) != null) {
                String str[] = s.split(" ");
                if(email.equals(str[1]))
                    return false;
            }
            buff.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return true;
    }
    //判断用户邮箱和密码是否正确
    public static User isRight(String email, String passwd)
    {
        User user = null;
        try
        {
            File file = new File("user.txt");
            BufferedReader buff = new BufferedReader(new FileReader(file));
            String s = "";
            while((s = buff.readLine()) != null)
            {
                String str[] = s.split(" ");
                if(email.equals(str[1]) && passwd.equals(str[2]))
                {
                    user = new User(str[0], str[1], str[2],str[3], str[4]);
                    return user;
                }
            }
            buff.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
        return null;
    }
    //判断莞工id与密码是否匹配存在
    public static User isRight_dgut(String id, String passwd)
    {
        User user = null;
        try
        {
            File file = new File("user.txt");
            BufferedReader buff = new BufferedReader(new FileReader(file));
            String s = "";
            while((s = buff.readLine()) != null)
            {
                String str[] = s.split(" ");
                if(id.equals(str[3]) && passwd.equals(str[2]))
                {
                    user = new User(str[0], str[1], str[2], str[3], str[4]);
                    break;
                }
            }
            buff.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return user;
        }
        return user;
    }
    //将注册好的用户信息写入文本
    public static boolean writeUser(String name, String email, String passwd)
    {
        try
        {
            File file = new File("user.txt");
            BufferedWriter buff = new BufferedWriter(new FileWriter(file,true));
            String s = name + " " + email + " " + passwd + " " + null +  " " + "userimags/0.jpg";;
            buff.write(s + '\n');
            buff.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    //为莞工登陆的用户创建账户并写入文本
    public static User writeDgut(String name, String id, String passwd)
    {
        User user = null;
        try
        {
            File file = new File("user.txt");
            BufferedWriter buff = new BufferedWriter(new FileWriter(file, true));
            String s = name + " " + null + " " + passwd +  " " + id + " " + "userimags/0.jpg";
            user = new User(name, null, passwd, id, "userimags/0.jpg");
            buff.write(s + '\n');
            buff.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
        return user;
    }
    //上传用户图片
    public static boolean uploadimg(User user)
    {
        BufferedReader br = null;
        BufferedWriter bw = null;
        String s1, s2;
        String line;
        List<String> list = new ArrayList<>();
        try
        {
            br = new BufferedReader(new FileReader("user.txt"));

            while((s1 = br.readLine()) != null)
            {
                line = s1;
                String str[] = s1.split(" ");
                if(user.getEmail().equals(str[1]) || user.getName().equals(str[3]))
                {
                    s2 = str[0] + " " + str[1] + " " + str[2] + " " + str[3] + " " + user.getImg();
                    line = s2;
                }
                list.add(line);
            }
            br.close();
        }
        catch (Exception e)
        {
            return false;
        }

        try
        {
            bw = new BufferedWriter(new FileWriter("user.txt"));

            for(String s : list)
                bw.write(s + "\r\n");
            bw.close();
        }
        catch (Exception e)
        {
            return false;
        }

        return true;
    }
}
